/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.JobDetail;
import client.entity.TempRecruimentCategory;
import client.entity.TempRecruimentLocation;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class LoadJobById implements Serializable {

    private JobDetail jobdetail;
    private int id;

    /**
     * Creates a new instance of LoadJobById
     */
    public LoadJobById() {
    }

    public JobDetail getJobdetail() {
        List<JobDetail> list = new ArrayList<JobDetail>();
        String pr = getParam("idJobDetail");
        if (pr == null) {
            id = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("idJobDetail");
        } else {
            id = Integer.parseInt(pr);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("idJobDetail", id);
        }
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        List result = ss.createSQLQuery("SELECT re.ID,c.Category_Name AS Category_Name,cu.Logo AS Category_Logo,re.Title AS Title,cu.Name AS Customer_Name,cu.ADDRESS AS Customer_Address,cu.Phone AS Customer_Phone,"
                + "cu.DESCRIPTION AS Customer_Description,re.Contact_Person AS Contact_Person,re.Email AS Email,re.Exprited_Date AS Exprited_Date,"
                + "re.Job_Description AS Job_Description,re.Job_Requiment AS Job_Requiment,re.Number,re.Benefits AS Benefits,lj.Name AS Level_Job_Name,"
                + "lo.Name AS Location_Name, re.Type_Of_Job FROM Recruitment re INNER JOIN Recruitment_Category rc ON re.ID=rc.ID_Recruitment INNER JOIN Category c ON c.ID=rc.ID_Category INNER JOIN Customer cu "
                + "ON cu.ID=re.ID_Customer INNER JOIN Level_Job lj ON lj.ID=re.ID_Level_Job INNER JOIN Recruitment_Location rl ON rl.ID_Recruitment=re.ID "
                + "INNER JOIN Location lo ON lo.ID=rl.ID_Location WHERE re.ID=" + id).addScalar("ID", Hibernate.INTEGER)
                .addScalar("Category_Name", Hibernate.STRING).addScalar("Category_Logo", Hibernate.STRING).addScalar("Title", Hibernate.STRING).addScalar("Customer_Name", Hibernate.STRING)
                .addScalar("Customer_Address", Hibernate.STRING).addScalar("Customer_Phone", Hibernate.STRING).addScalar("Customer_Description", Hibernate.STRING)
                .addScalar("Contact_Person", Hibernate.STRING).addScalar("Email", Hibernate.STRING).addScalar("Exprited_Date", Hibernate.STRING).addScalar("Job_Description", Hibernate.STRING)
                .addScalar("Job_Requiment", Hibernate.STRING).addScalar("Number", Hibernate.INTEGER).addScalar("Benefits", Hibernate.STRING)
                .addScalar("Level_Job_Name", Hibernate.STRING).addScalar("Location_Name", Hibernate.STRING)
                .addScalar("Type_Of_Job", Hibernate.BOOLEAN).list();
        for (Object object : result) {
            jobdetail = new JobDetail();
            Object[] lstO = (Object[]) object;
            jobdetail.setId(Integer.parseInt(lstO[0].toString()));
            if (lstO[2] == null) {
                jobdetail.setLogo("LogoW.png");
            } else {
                jobdetail.setLogo(lstO[2].toString());
            }
            jobdetail.setTitleJob(lstO[3].toString());
            jobdetail.setNameCus(lstO[4].toString());
            jobdetail.setAddress(lstO[5].toString());
            jobdetail.setPhone(lstO[6].toString());
            jobdetail.setCusDescription(lstO[7].toString());
            jobdetail.setContactPerson(lstO[8].toString());
            jobdetail.setEmail(lstO[9].toString());
            jobdetail.setExperiedDate(lstO[10].toString());
            jobdetail.setJobDescription(lstO[11].toString());
            jobdetail.setRequirement(lstO[12].toString());
            jobdetail.setNumber(Integer.parseInt(lstO[13].toString()));
            jobdetail.setBenefit(lstO[14].toString());
            jobdetail.setJobLevel(lstO[15].toString());
            //jobdetail.setJobLocation(lstO[16].toString());
            if (lstO[17].toString().equals("1")) {
                jobdetail.setTypeOfJob("Full Time");
            } else {
                jobdetail.setTypeOfJob("Past Time");
            }
            List<TempRecruimentCategory> listCateOfJob = new ArrayList<TempRecruimentCategory>();
            List tempRs = ss.createSQLQuery("SELECT rc.ID_Recruitment,rc.ID_Category,c.Category_Name  FROM Recruitment_Category rc INNER JOIN Category c ON rc.ID_Category=c.ID WHERE rc.ID_Recruitment=" + id)
                    .addScalar("ID_Recruitment", Hibernate.INTEGER).addScalar("ID_Category", Hibernate.INTEGER)
                    .addScalar("Category_Name", Hibernate.STRING).list();
            for (Object objectRc : tempRs) {
                TempRecruimentCategory rc = new TempRecruimentCategory();
                Object[] lstOrc = (Object[]) objectRc;
                rc.setIdrecruiment(Integer.parseInt(lstOrc[0].toString()));
                rc.setIdcategory(Integer.parseInt(lstOrc[1].toString()));
                rc.setNamecate(lstOrc[2].toString());
                listCateOfJob.add(rc);
            }
            List<TempRecruimentLocation> listLocaleOfJob = new ArrayList<TempRecruimentLocation>();
            List tempRsLc = ss.createSQLQuery("SELECT rc.ID_Recruitment,rc.ID_Location,lc.Name  FROM Recruitment_Location rc INNER JOIN Location lc ON rc.ID_Location=lc.ID WHERE rc.ID_Recruitment=" + id)
                    .addScalar("ID_Recruitment", Hibernate.INTEGER).addScalar("ID_Location", Hibernate.INTEGER)
                    .addScalar("Name", Hibernate.STRING).list();
            for (Object objectRc : tempRsLc) {
                TempRecruimentLocation rl = new TempRecruimentLocation();
                Object[] lstOrc = (Object[]) objectRc;
                rl.setIdrecruiment(Integer.parseInt(lstOrc[0].toString()));
                rl.setIdlocation(Integer.parseInt(lstOrc[1].toString()));
                rl.setNamelocation(lstOrc[2].toString());
                listLocaleOfJob.add(rl);
            }
            jobdetail.setListLocaleOfJob(listLocaleOfJob);
            jobdetail.setListCateOfJob(listCateOfJob);
            list.add(jobdetail);
        }
        ss.getTransaction().commit();
        ss.close();
        return list.get(0);
    }

    public void setJobdetail(JobDetail jobdetail) {
        this.jobdetail = jobdetail;
    }

    public String getParam(String param) {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public String backToProfile() {
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("customerProfile");
        return "customerProfile.xhtml?accId=" + 27 + "faces-redirect=true";
    }
}
